package com.zjuee.web.company.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.core.utils.RetryingUtil;
import com.zjuee.gateway.utils.EncryptUtil;
import com.zjuee.service.company.service.CompanyLicenseClassService;
import com.zjuee.service.company.service.CompanyLicenseGroupService;
import com.zjuee.service.company.service.CompanyLicenseService;
import com.zjuee.service.company.vo.CompanyLicenseVo;
import com.zjuee.service.company.vo.CompanyOtherProVo;
import com.zjuee.base.model.common.company.CompanyLicense;
import com.zjuee.web.company.mapper.CompanyLicenseMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CompanyLicenseServiceImpl extends ServiceImpl<CompanyLicenseMapper, CompanyLicense> implements CompanyLicenseService {

    @Resource
    private CompanyLicenseMapper companyLicenseMapper;

    @Resource
    private CompanyLicenseClassService licenseClassService;

    @Resource
    private CompanyLicenseGroupService licenseGroupService;

    @Resource
    private LoginDto loginDto;


    @Override
    public List<CompanyLicenseVo> selectByCompanyId(Long id) {
        //判断企业Id是否正确
//        if(StringUtils.equalsIgnoreCase(loginDto.getOrgBusinessId().toString(),
//                id.toString())){
//            throw new BaseException(CommonResultCode.CONSUMER_ERR, "请确认企业Id是否错误");
//        }


        return completeList(companyLicenseMapper.getLicenseInfo(id));
    }

    @Override
    public CompanyOtherProVo getCompanyOtherProInfo(String jqxkzbh) {
        //组装数据
        JSONObject jsonObject = new JSONObject();
        jsonObject
                .put("xkzzt", "1");
        jsonObject.put("", "");
        //发送全国平台
        String namespace= "http://service.transfer.dataInsert.webService.core.g20.streetlight.org";
        String key="226f8e2c0bbb812b";
        String vi="1234567812345678";
        String timeOut="100000";
        String method = "getXkz";
        String encrypt=null;
        try {
            //加密
            encrypt = EncryptUtil.encrypt(key, vi, jsonObject.toJSONString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        RetryingUtil retryingUtil = new RetryingUtil();
        String jsonResult = retryingUtil
                .send(encrypt, key,
                        "http://gfxt.mepscc.cn/edpgf_wsgrade_test/servicesx/KSTranService?wsdl",
                        method);
        log.info(jsonResult);
        return null;
    }

    @Override
    public List<CompanyLicenseVo> listAll(CompanyLicense companyLicense) {
        return completeList(companyLicenseMapper.findLicenseInfo(companyLicense));
    }

    private List<CompanyLicenseVo>  completeList(List<CompanyLicenseVo> licenseList){
        licenseList = licenseList.stream().peek(x -> {
            //查询详细信息
            List<CompanyLicenseVo> licenseInfoList = licenseGroupService.getMinuteInfo(x.getId());
            //设置数据
            x.setChildren(licenseInfoList);
        }).collect(Collectors.toList());

        return licenseList;
    }



}
